Setting Up TD Ameritrade API

1) Register at https://developer.tdameritrade.com/

2) Go to my Apps and create an App

3) Give the App a name and user id, set the Callback URL to http://127.0.0.1, and add a description

4) Click on My Apps
- Click on the name of your App
- Open Notepad and paste the CONSUMER KEY (you will need this later)
Example: QBGZI44GH6799345IPYIMWXYZ

5) Make sure Windows Local Host is turned on
Control panel > Programs > Turn Windows features on or Off >
Internet information Services
Internet information Services Hostable Web core

6) Retrieve Encoded Authorization Code ("EAC")
- Browse to: https://auth.tdameritrade.com/auth?response_type=code&redirect_uri=http://127.0.0.1&client_id=<Your CONSUMER KEY>%40AMER.OAUTHAP
Example: https://auth.tdameritrade.com/auth?response_type=code&redirect_uri=http://127.0.0.1&client_id=QBGZI44GH6799345IPYIMWXYZ%40AMER.OAUTHAP
- Enter your account credentials. This process will result in an error page.
- Copy everything in the address bar that comes after "code=" .It is a long series of numbers and letters. This is your Encoded Authorization Code ("EAC").
Paste the EAC into Notepad for later.

7) Calculate the Decoded Authorization Code ("DAC")
- Browse to the online dencoder at: https://meyerweb.com/eric/tools/dencoder/
- Paste the EAC into the window and click Decode
- Copy and paste the Decoded Authorization Code ("DAC") into Notepad for later.

8) Get Access Tokens
- Browse to: https://developer.tdameritrade.com/authentication/apis/post/token-0
- Enter the following:

Grant Type: authorization_code
Access Type: offline
Code: <Your DAC>
Client ID: <Your CONSUMER KEY>@AMER.OAUTHAP
Example: QBGZI44GH6799345IPYIMWXYZ@AMER.OAUTHAP
Redirect_uri: http://127.0.0.1


- Click SEND

You should now see decoded Access Token ("AT") and Refresh Token ("RT") in a long string

9) Reencode and record the AT and RT in Excel
- Copy and paste the AT into the online dencoder and encode it
- Paste the encoded AT into cell B3 of the Settings tab on the Excel spreadsheet
- Copy and paste the RT into the online dencoder and encode it
- Paste the encoded RT into cell B4 of the Settings tab on the Excel spreadsheet
- Don't forget to also paste your CONSUMER KEY and TD Ameritrade trading account number into the appropriate cells on the Settings tab.

The AT is good for 30 minutes
The RT is good for 90 days

The RT is used to request a new AT when you need one.
The Spreadsheet keeps track of how long you've used a token and will automatically request new tokens when needed.
Sometimes you will need to go through the above process again if the tokens expire for any reason.